﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ECare.Client.templates;
using ECare.BUS.Data;
using ECare.BUS;

namespace ECare.Client.frms
{
    public partial class frmStackers : frmGridTemplate
    {
        public frmStackers()
        {
            InitializeComponent();
            this.Load += frmStackers_Load;

            this.grd.CellDoubleClick += grd_CellDoubleClick;
        }

        void grd_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            var entitye = this.grd.Rows[e.RowIndex].DataBoundItem as StackerInfo;
            var frm = new frmStackerInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }


        public BindingList<StackerInfo> source { get; set; }
        void frmStackers_Load(object sender, EventArgs e)
        {
            source = new BindingList<StackerInfo>(StackerInfoHelper.GetStackers());
            this.grd.DataSource = source;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var frm = new frmStackerInfo();
            frm.entity = null;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }
        void frm_Saved(object entity)
        {
            if (source == null)
                source = new BindingList<StackerInfo>();
            var role = entity as StackerInfo;
            var findresult = source.FirstOrDefault(a => a.StackerInfo_Id == role.StackerInfo_Id);
            if (findresult == null)
                this.source.Add(role);

        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.grd.SelectedRows.Count == 0)
                return;

            var entitye = this.grd.SelectedRows[0].DataBoundItem as StackerInfo;
            var frm = new frmStackerInfo();
            frm.entity = entitye;
            frm.Saved += frm_Saved;
            frm.ShowDialog();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

        }
    }
}
